package com.glhtgyxy.biyesheji.Service;

import com.glhtgyxy.biyesheji.Mapper.orderMapper;
import com.glhtgyxy.biyesheji.Pojo.house;
import com.glhtgyxy.biyesheji.Pojo.house_time;
import com.glhtgyxy.biyesheji.Pojo.order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class orderService {

    @Autowired
    orderMapper mapper;

    public ArrayList<order> findallorder(){
        return mapper.findallorder();
    }

    public ArrayList<house_time> finduserorder(int id){
        ArrayList<order> finduserorder = mapper.finduserorder(id);
        ArrayList<house_time> houses = new ArrayList<>();
        for (int i = 0; i < finduserorder.size(); i++) {
            int houseid = finduserorder.get(i).getHouseid();
            house hs = mapper.houseidfindhouse(houseid);
            house_time house_time = new house_time();
            house_time.setId(hs.getId());
            house_time.setHousename(hs.getHousename());
            house_time.setAddr(hs.getAddr());
            house_time.setPrice(hs.getPrice());
            house_time.setBedroom(hs.getBedroom());
            house_time.setFacilityservices(hs.getFacilityservices());
            house_time.setImage(hs.getImage());
            house_time.setMasterid(hs.getMasterid());
            house_time.setCheckInDate(finduserorder.get(i).getCheckInDate());
            house_time.setCheckOutDate(finduserorder.get(i).getCheckOutDate());
            houses.add(house_time) ;
        }
        return houses;
    }

    public ArrayList<house_time> findmerchantorder(int id){
        ArrayList<Integer> merchant_house_id = mapper.findmerchanthouse(id);
        System.out.println("merchant id "+merchant_house_id);
        ArrayList<house_time> houses = new ArrayList<>();
        ArrayList<order> merchant_order = new ArrayList<>();
        for (int i = 0 ; i < merchant_house_id.size() ; i ++)
        {
            merchant_order = mapper.houseid_find_order(merchant_house_id.get(i));
            for (int j = 0 ; j < merchant_order.size() ; j ++)
            {
                order order = merchant_order.get(j);
                house houseidfindhouse = mapper.houseidfindhouse(order.getHouseid());
                house_time housetime = new house_time();
                housetime.setId(houseidfindhouse.getId());
                housetime.setHousename(houseidfindhouse.getHousename());
                housetime.setAddr(houseidfindhouse.getAddr());
                housetime.setPrice(houseidfindhouse.getPrice());
                housetime.setBedroom(houseidfindhouse.getBedroom());
                housetime.setFacilityservices(houseidfindhouse.getFacilityservices());
                housetime.setImage(houseidfindhouse.getImage());
                housetime.setMasterid(houseidfindhouse.getMasterid());
                housetime.setCheckInDate(order.getCheckInDate());
                housetime.setCheckOutDate(order.getCheckOutDate());
                houses.add(housetime);
            }
        }
        return houses;
    }

    public boolean deleteuserorder(int userid,int houseid){
        return mapper.deleteuserorder(userid,houseid)>0?true:false;
    }


    public ArrayList<order> search(String  searchtext){
        return mapper.search(searchtext);
    }

    public boolean deleteorder(int id){
        return mapper.deleteorder(id)>0?true:false;
    }

    public boolean updateorder(int id,
                               int houseid,
                               int userid
    ){
        return mapper.updateorder(id,houseid,userid)>0?true:false;
    }

}
